﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DoAnTriTueNhanTao
{
    public partial class Form1 : Form
    {
        List<MyPoint> myPoint;
        List<MyLine> myLine;
        List<int> saveState;

        Sprite chartField;

        bool isDrawLine;
        bool movePoint;

        int index;
        int mouseX;
        int mouseY;

        MatrixInput matrixInput;

        public Form1()
        {
            InitializeComponent();
            myPoint = new List<MyPoint>();
            myLine = new List<MyLine>();
            saveState = new List<int>();

            chartField = new Sprite(Properties.Resources.ChartField);
            chartField.Left = 10;
            chartField.Top = 10;

            isDrawLine = false;
            movePoint = false;

            matrixInput = new MatrixInput();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            chartField.Draw(g);
            if (isDrawLine)
            {
                Pen p = new Pen(Color.Black);
                p.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;

                g.DrawLine(p, myPoint[index].getX(), myPoint[index].getY(), mouseX, mouseY);
            }
            for (int i = 0; i < myLine.Count; i++)
            {
                myLine[i].Draw(g);
            }
            for (int i = 0; i < myPoint.Count; i++)
            {
                myPoint[i].Draw(g);

                myPoint[i].DrawIndex(g, i);
            }
        }


        private void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (chartField.isClicked(e.X, e.Y))
            {
                for (int i = 0; i < myPoint.Count; i++)
                {
                    if (myPoint[i].isClicked(e.X, e.Y))
                    {
                        if (!movePoint)
                        {
                            index = i;
                            movePoint = true;
                            return;
                        }
                    }
                }
                movePoint = false;
            }
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (chartField.isClicked(e.X, e.Y))
            {
                if (isDrawLine)
                {
                    mouseX = e.X;
                    mouseY = e.Y;
                    Invalidate();
                    Refresh();
                }
                if (movePoint)
                {
                    myPoint[index].setPosition(e.X, e.Y);
                }
            }
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            if (chartField.isClicked(e.X, e.Y))
            {
                if (!movePoint)
                {
                    for (int i = 0; i < myPoint.Count; i++)
                    {
                        if (myPoint[i].isClicked(e.X, e.Y))
                        {
                            if (!isDrawLine)
                            {
                                index = i;
                                isDrawLine = true;
                            }
                            else
                            {
                                CostInput costInput = new CostInput();
                                costInput.ShowDialog();
                                if (PublicAndStatic.Cost != -1)
                                {
                                    saveState.Add(1);
                                    MyLine line = new MyLine(myPoint[index], myPoint[i]);
                                    line.cost = PublicAndStatic.Cost;
                                    line.idxStartPoint = index;
                                    line.idxEndPoint = i;
                                    myLine.Add(line);
                                    isDrawLine = false;
                                    PublicAndStatic.Cost = -1;
                                    Invalidate();
                                    Refresh();
                                }
                                else
                                {
                                    isDrawLine = false;
                                    Invalidate();
                                    Refresh();
                                }
                            }
                            return;
                        }
                    }

                    if (!isDrawLine)
                    {
                        HeuristicInput hInput = new HeuristicInput();
                        hInput.ShowDialog();
                        if (PublicAndStatic.Heuristic != -1)
                        {
                            saveState.Add(0);
                            MyPoint point = new MyPoint(e.X, e.Y);
                            point.heuristic = PublicAndStatic.Heuristic;
                            myPoint.Add(point);
                        }
                    }

                    isDrawLine = false;
                    Invalidate();
                    Refresh();
                }
            }
        }

        private void button_run_Click(object sender, EventArgs e)
        {
            //matrixInput.makeSampleInput();
            getInput();
        }
        private void getInput()
        {
            int start, end;
            try
            {
                start = int.Parse(textBox_startPoint.Text);
                end = int.Parse(textBox_endPoint.Text);
            }
            catch
            {
                MessageBox.Show("Điểm đầu hoặc điểm cuối không hợp lệ!");
                return;
            }

            matrixInput = new MatrixInput();
            matrixInput.startPoint = start;
            matrixInput.endPoint = end;
            matrixInput.nPoint = myPoint.Count;
            matrixInput.matrix = new int[myPoint.Count, myPoint.Count];
            matrixInput.heuristic = new int[myPoint.Count];
            for (int i = 0; i < myPoint.Count; i++)
            {
                matrixInput.heuristic[i] = myPoint[i].heuristic;
            }
            for (int i = 0; i < myPoint.Count; i++)
                for (int j = 0; j < myPoint.Count; j++)
                {
                    matrixInput.matrix[i, j] = -1;
                }
            for (int i = 0; i < myLine.Count; i++)
            {
                matrixInput.matrix[myLine[i].idxStartPoint, myLine[i].idxEndPoint] = myLine[i].cost;
            }
        }

        private void button_undo_Click(object sender, EventArgs e)
        {
            if (saveState.Count > 0)
            {
                if (saveState[saveState.Count - 1] == 0)
                {
                    myPoint.RemoveAt(myPoint.Count - 1);
                }
                else
                {
                    myLine.RemoveAt(myLine.Count - 1);
                }
                saveState.RemoveAt(saveState.Count - 1);
                Invalidate();
                Refresh();
            }
        }
    }
}
